﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SaveShoutout')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SaveShoutout'
    DROP  Procedure  proc_cm_SaveShoutout
END
GO

PRINT 'Creating Procedure proc_cm_SaveShoutout'
GO

CREATE PROCEDURE [dbo].[proc_cm_SaveShoutout]
	@pShoutoutId uniqueidentifier
	, @pHtmlPageId uniqueidentifier
	, @pSectionCd nvarchar(50) = 'Main'
	, @pUserName nvarchar(255)
	, @pContentTxt nvarchar(1024)
AS
BEGIN
	DECLARE @vUserId UNIQUEIDENTIFIER

	SELECT @vUserId = UserId
	FROM aspnet_Users (nolock)
	WHERE UserName = @pUserName
	
	UPDATE
		[dbo].[tbl_shoutout]
	SET 
		[content_txt] = @pContentTxt
		,[UserId] = @vUserId
		,[update_dttm] = GETDATE()
	WHERE
		[shoutout_id] = @pShoutoutId
		AND [html_page_id] = @pHtmlPageId
		AND [section_cd] = @pSectionCd

	IF @@ROWCOUNT = 0
	BEGIN
		INSERT INTO [dbo].[tbl_shoutout]
		(
			[shoutout_id]
			,[html_page_id]
			,[content_txt]
			,[UserId]
			,[create_dttm]
			,[update_dttm]
			,[section_cd]
		)
		SELECT
			@pShoutoutId
			, @pHtmlPageId
			, @pContentTxt
			, @vUserId
			, GETDATE()
			, GETDATE()
			, @pSectionCd
	END
END
GO

GRANT EXEC ON dbo.proc_cm_SaveShoutout TO PUBLIC
GO
